package com.huadao.pda.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.huadao.pda.R;
import com.huadao.pda.common.RockToast;
import com.huadao.pda.common.dialog.DialogFragmentHelper;
import com.jaeger.library.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity implements BaseView {
    private ProgressDialog mProgressDialog;
    protected final String TAG = getClass().getSimpleName();
//    private ProgressDialogHandler mProgressDialogHandler;

    protected Toolbar toolbar;
    protected View back;
    protected View close;
    protected TextView title;

    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        com.huadao.pda.common.Immerse.StatusBarUtil.setRootViewFitsSystemWindows(this,true);
        //设置状态栏透明
        com.huadao.pda.common.Immerse.StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
//        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
//            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
//            //这样半透明+白=灰, 状态栏的文字能看得清
//            StatusBarUtil.setStatusBarColor(this,0x55000000);
//        }

        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCancelable(true);
        Window window=mProgressDialog.getWindow();
        WindowManager.LayoutParams layoutParams=window.getAttributes();
        layoutParams.gravity=Gravity.CENTER;
        window.setAttributes(layoutParams);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        mProgressDialogHandler=new ProgressDialogHandler(this,true);
        setContentView(getContentLayoutId());
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.blue));
        unbinder=ButterKnife.bind(this);
        initActionBar();
        init();
    }

    protected abstract void init();

    protected abstract int getContentLayoutId();

    protected void initActionBar() {
        toolbar = findViewById(R.id.toolbar);
        if (toolbar != null) {
            back=findViewById(R.id.back);
            title=findViewById(R.id.title);
            close=findViewById(R.id.close);
            if(back!=null) {
                back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBack();
                    }
                });
            }
            if(close!=null) {
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClose();
                    }
                });
            }
            setSupportActionBar(toolbar);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayShowTitleEnabled(false);
            }
        }
    }

    private void onClose() {
        finish();
    }

    protected void onBack() {
        finish();
    }

    @Override
    public void showLoading(String msg) {
//        if (mProgressDialogHandler != null) {
//            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG, msg).sendToTarget();
//        }
        if (!mProgressDialog.isShowing()) {
            if(TextUtils.isEmpty(msg)) {
                msg=getString(R.string.loading);
            }
            mProgressDialog.setMessage(msg);
            mProgressDialog.show();
        }
    }
    @Override
    public void hideLoading() {
//        if (mProgressDialogHandler != null) {
//            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
//        }
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void showToast(String msg) {
        RockToast.showToastSafe(msg);
    }

    @Override
    public void showDialogMessage(String title,String msg) {
        if(TextUtils.isEmpty(title)){
            title="";
        }
        DialogFragmentHelper.showMessageDialog(this,title,msg);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                View view = getCurrentFocus();
                BaseHelper.hideKeyboard(ev, view, BaseActivity.this);//调用方法判断是否需要隐藏键盘
                break;

            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        hideLoading();
        unbinder.unbind();
        super.onDestroy();
    }

}
